<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 搜索区域 ：input绑定变量query，获取input里面的内容-->
        <div class="filter-bar">
          <div class="l-form">
            <el-form label-width="80px" :inline="true" size="small" v-model="info">
              <el-form-item label="标签名称">
                <el-input placeholder="请输入" v-model="queryInfo.tags"></el-input>
              </el-form-item>
              <el-form-item label="城市">
                <el-select v-model="queryInfo.province" placeholder="请选择" @change="cityChange(queryInfo.province)">
                  <el-option v-for="item in cs" :key="item" :label="item" :value="item"> </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="地区">
                <el-select v-model="queryInfo.city" placeholder="请选择">
                  <el-option v-for="item in cityList" :key="item" :label="item" :value="item"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="企业名称">
                <el-input placeholder="请输入" v-model="queryInfo.shortName"></el-input>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="state" placeholder="请选择">
                  <el-option label="禁用" :value="0"></el-option>
                  <el-option label="启用" :value="1"> </el-option>
                </el-select>
              </el-form-item>

              <el-form-item>
                <el-button @click="resetSearch">清除</el-button>
                <el-button type="primary" @click="getOrderList(state)">搜索</el-button>
              </el-form-item>
              <el-button type="success" style="float: right" @click="showAddress">新增用户</el-button>
            </el-form>

            <el-alert type="info" left show-icon> 共{{ total }}条记录</el-alert>
            <!-- 表格区域 -->
            <div class="table-box">
              <!-- 订单表格 -->
              <el-table :data="orderList" border stripe>
                <el-table-column label="序号" prop="id"></el-table-column>
                <el-table-column label="企业编号" prop="number"></el-table-column>
                <el-table-column label="企业简称" prop="company"></el-table-column>
                <el-table-column label="标签" prop="tags"></el-table-column>
                <el-table-column label="创建者" prop="creatorID"></el-table-column>
                <el-table-column label="创建日期" prop="addDate"></el-table-column>
                <el-table-column label="备注" prop="remarks"></el-table-column>
                <el-table-column label="状态" prop="state">
                  <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.state === 1">启用</el-tag>
                    <el-tag type="danger" v-else>禁用</el-tag>
                  </template>
                </el-table-column>

                <el-table-column label="操作" width="200px">
                  <template slot-scope="scope">
                    <el-button size="mini" type="success" icon="el-icon-edit" @click="showEditAddress(scope.row)" circle></el-button>

                    <el-tooltip class="item" effect="dark" content="启用" placement="top" v-if="scope.row.state === 0">
                      <el-button size="mini" type="primary" icon="el-icon-check" @click="changeStateClose(scope.row)" circle></el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="禁用" placement="top" v-else>
                      <el-button size="mini" type="primary" icon="el-icon-close" @click="changeStateOpen(scope.row)" circle></el-button>
                    </el-tooltip>

                    <el-button size="mini" type="warning" icon="el-icon-delete" @click="removeExpress(scope.row)" circle></el-button>
                  </template>
                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.page"
                :page-sizes="[3, 5, 10, 15]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>

              <!-- 修改用户弹框 -->

              <el-dialog title="修改企业" :visible.sync="editDiaologShow" width="50%">
                <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                  <!-- 表单 -->
                  <el-form-item label="企业名称" prop="company" label-width="100px">
                    <el-input v-model="editForm.company"></el-input>
                  </el-form-item>

                  <el-form-item label="所属公司" prop="shortName" label-width="100px">
                    <el-input v-model="editForm.shortName"></el-input>
                  </el-form-item>

                  <el-form-item label="城市" class="cs" prop="province">
                    <el-select v-model="editForm.province" placeholder="请选择" @change="cityChange(editForm.province)">
                      <el-option v-for="item in cs" :key="item" :label="item" :value="item"> </el-option>
                    </el-select>
                    <el-select v-model="editForm.city" placeholder="请选择" collapse-tags style="margin-left: 20px">
                      <el-option v-for="item in cityList" :key="item" :label="item" :value="item"> </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item label="方向(企业标签)" prop="tags" label-width="135px">
                    <el-input v-model="editForm.tags"></el-input>
                  </el-form-item>
                  <el-form-item label="备注" prop="remarks"> <el-input type="textarea" autosize placeholder="请输入内容" v-model="editForm.remarks"> </el-input></el-form-item>
                </el-form>
                <!-- 对话框底部区域 -->
                <span slot="footer" class="dialog-footer">
                  <el-button @click="editDiaologShow = false">取 消</el-button>
                  <el-button type="primary" @click="editUserSubmit">确 定</el-button>
                </span>
              </el-dialog>

              <!-- 增加用户弹框 -->
              <el-dialog title="增加企业" :visible.sync="addDiaologShow" width="50%">
                <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                  <el-form-item label="企业名称" prop="company" label-width="100px">
                    <el-input v-model="addForm.company"></el-input>
                  </el-form-item>

                  <el-form-item label="所属公司" prop="shortName" label-width="100px">
                    <el-input v-model="addForm.shortName"></el-input>
                  </el-form-item>
                  <el-form-item label="城市" prop="province">
                    <el-select v-model="addForm.province" placeholder="请选择" @change="cityChange(addForm.province)">
                      <el-option v-for="item in cs" :key="item" :label="item" :value="item"> </el-option>
                    </el-select>
                    <el-select v-model="addForm.city" placeholder="请选择" collapse-tags style="margin-left: 20px">
                      <el-option v-for="item in cityList" :key="item" :label="item" :value="item"> </el-option>
                    </el-select>
                  </el-form-item>

                  <el-form-item label="方向(企业标签)" prop="tags" label-width="135px">
                    <el-input v-model="addForm.tags"></el-input>
                  </el-form-item>
                  <el-form-item label="备注" prop="remarks"> <el-input type="textarea" autosize placeholder="请输入内容" v-model="addForm.remarks"> </el-input></el-form-item>
                </el-form>

                <span slot="footer" class="dialog-footer">
                  <el-button @click="addDiaologShow = false">取 消</el-button>
                  <el-button type="primary" @click="addSubmit">确 定</el-button>
                </span>
              </el-dialog>

              <!-- <companys-add :add="addDiaologShow" @close="getOrderList" /> -->
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { provinces, citys } from './../../api/hmmm/citys'
import { list, remove, update, add, disabled } from '../../api/hmmm/companys'
import CompanysAdd from '../components/companys-add'

let defaultItem = {
  page: 1,
  pagesize: 10,
  tags: '',
  province: '',
  city: '',
  shortName: ''
  // state: 1
}
let addclone = {
  id: '',
  number: '',
  isFamous: false,
  shortName: '',
  company: '',
  province: '',
  city: '',
  tags: '',
  remarks: ''
}
export default {
  name: 'CompanysList',
  components: {
    CompanysAdd
  },
  data() {
    return {
      cityList: [],
      info: '',
      //查询条件
      queryInfo: { ...defaultItem },
      //订单列表数据
      orderList: [],
      //数据总条数
      total: 0,
      state: '',

      //   是否显示添加用户弹出窗
      addDiaologShow: false,
      //   是否显示添加用户弹出窗
      editDiaologShow: false,
      //   添加用户的表单数据
      addForm: {
        ...addclone
      },
      //   修改用户的表单数据
      editForm: {
        id: '',
        number: '',
        isFamous: false,
        shortName: '',
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      },

      //   修改表单的验证规则对象
      editFormRules: {
        company: [
          { required: true, message: '请输入内容', trigger: 'blur' }
          // {
          //   min: 3,
          //   max: 10,
          //   message: '用户名在3~10个字符之间',
          //   trigger: 'blur'
          // }
        ],
        shortName: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        province: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        tags: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      //   增加表单的验证规则对象
      addFormRules: {
        company: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        shortName: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        province: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        tags: [{ required: true, message: '请输入内容', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  computed: {
    cs: provinces
  },
  created() {
    // console.log(provinces)
    this.getOrderList()
  },
  methods: {
    async getOrderList(state) {
      let query = this.queryInfo
      let fv = { ...query, state }
      console.log(fv)
      // this.queryInfo = { ...defaultItem }
      try {
        const { data: res } = await list(fv)
        this.orderList = res.items
        console.log(res)
        this.total = res.counts
      } catch (error) {
        this.$toast.fail('获取用户资料失败')
      }
    },
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.page = newPage
      this.getOrderList()
    },
    async removeExpress(data) {
      // console.log(data)
      try {
        await this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await remove(data)
        // console.log(res)
        if (res.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        if (this.queryInfo.page !== 1 && this.orderList.length === 1) {
          this.queryInfo.page--
        }
        this.getOrderList()
      } catch (error) {
        this.$message.info('取消删除')
      }
    },
    async showEditAddress(row) {
      // console.log(row)
      // let { data: res } = await this.$axios.get('users/' + id)

      this.editForm.id = row.id
      this.editForm.company = row.company
      this.editForm.shortName = row.shortName
      this.editForm.province = row.province
      this.editForm.city = row.city
      this.editForm.tags = row.tags
      this.editForm.remarks = row.remarks
      this.editForm.number = row.number
      if (row.isFamous == 0) {
        this.editForm.isFamous = false
      } else {
        this.editForm.isFamous = true
      }

      // console.log(this.editForm)
      this.editDiaologShow = true
    },

    async editUserSubmit() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        //  发送请求完成添加用户的操作
        let res = await update(this.editForm)
        // 判断如果添加失败，就做提示
        // console.log(res)
        if (res.status !== 200) return this.$message.error('修改失败')
        // 添加成功的提示
        this.$message.success('修改成功')
        // 重新请求最新的数据
        this.getOrderList()
        // 关闭对话框
        this.editDiaologShow = false
      })
    },
    showAddress() {
      this.addDiaologShow = true
    },
    async addSubmit() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        //  发送请求完成添加用户的操作
        let res = await add(this.addForm)
        // 判断如果添加失败，就做提示
        console.log(res)
        if (res.status !== 200) return this.$message.error('添加失败')
        // 添加成功的提示
        this.$message.success('添加成功')
        this.addForm = { ...addclone }
        // 重新请求最新的数据
        this.getOrderList()
        // 关闭对话框
        this.addDiaologShow = false
      })
    },
    cityChange(val) {
      // console.log(val)
      this.cityList = []
      this.queryInfo.city = ''
      this.editForm.city = ''
      this.addForm.city = ''
      this.cityList = citys(val)
      // console.log(this.cityList)
    },
    resetSearch() {
      this.queryInfo = { ...defaultItem }
      this.state = ''
    },
    async changeStateClose(row) {
      // console.log(row.state)

      let gg = { id: row.id, state: 1 }
      let res = await disabled(gg)
      // 判断如果添加失败，就做提示
      // console.log(res)
      if (res.status !== 200) return this.$message.error('启用失败')
      // 添加成功的提示
      this.$message.success('启用成功')
      // 重新请求最新的数据

      if (!row.state) {
        row.state = 1
      }
      // this.getOrderList()
    },
    async changeStateOpen(row) {
      // console.log(row.state)
      let ff = { id: row.id, state: 0 }
      let res = await disabled(ff)
      // 判断如果添加失败，就做提示
      // console.log(res)
      if (res.status !== 200) return this.$message.error('禁用失败')
      // 添加成功的提示
      this.$message.success('禁用成功')

      if (row.state) {
        row.state = 0
      }
      // console.log(row.state)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-alert {
  margin-bottom: 20px;
}
</style>
